package com.lkm.market2_0.m.steelyarddata;

import com.google.gson.annotations.SerializedName;
import com.lkm.javatool.CollectionHelp;
import com.lkm.javatool.StringHelp;
import com.lkm.market2_0.entity.BreedTo;
import com.lkm.market2_0.entity.SteelyardInfo;
import com.lkm.market2_0.help.SteelardHelp;
import com.lkm.market2_0.m.steelyardserver.to.WeightChangeInfo;

import java.util.List;


public class OrderEntity {


    public static final int TYPE_NotFull0     = 0;
    public static final int TYPE_Full1        = 1;
    public static final int TYPE_SalesReturn2 = 2;

    public static final int TYPE_History3   = 3;
    public static final int TYPE_SalesJoin4 = 4;


    public static OrderEntity createOrderEntity(String steelyardMac, int type, String createTime, List<OrderEntity.GoodEntity> goodEntitys, String orderNum) {

        OrderEntity orderEntity = new OrderEntity(steelyardMac, type, createTime, goodEntitys, orderNum);

        try {

            if (!StringHelp.isEmpty(orderNum) && orderEntity.getGoods() != null)
                for (OrderEntity.GoodEntity goodEntity : orderEntity.getGoods()) {
                    goodEntity.img_on = orderNum + "#" + goodEntity.itemId + ".jpg";
                }
        } catch (Exception e) {
        }

        return orderEntity;
    }


    private OrderEntity(String steelyardMac, int type, String createTime, List<GoodEntity> goods, String orderNum) {
        this.type = type;

        this.goods = goods;
        this.createTime = createTime;
        this.steelyardMac = steelyardMac;
        this.orderNum = orderNum;

        float totalPrice = 0.0f;

        if (!CollectionHelp.isEmpty(goods))
            for (OrderEntity.GoodEntity ge : goods) {
                totalPrice += ge.calPrice();//SteelardHelp.formatMoneyFloat(ge.unitPrice * ge.weight);
            }

        this.totalPrice = SteelardHelp.formatMoneyFloat(totalPrice);
    }

    public boolean isFullOrder() {
        return this.type == TYPE_Full1;
    }

    public boolean isSalesReturn() {
        return this.type == TYPE_SalesReturn2;
    }


    @SerializedName("od")
    public String orderNum;


    @SerializedName("ty")
    public int    type;
    public String payTypeName = "";

    @SerializedName("tp")
    public float totalPrice;


    @SerializedName("payment_amount")
    public String payment_amount;


    @SerializedName("pi")
    public String payInfo = "";


    @SerializedName("card")
    public String card = "";

    @SerializedName("gs")
    private List<GoodEntity> goods;

    public List<GoodEntity> getGoods() {
        return goods;
    }


    @SerializedName("ct")
    public String createTime;


    @SerializedName("ukey")
    public String steelyardMac;


    public static class GoodEntityEx {


        public transient Object fj_Info;

    }


    public static class GoodEntity {
        public volatile String checkResult;

        public static OrderEntity.GoodEntity toGoodEntity(SteelyardInfo steelyardInfo, WeightChangeInfo saveSaleCacheInfo, GoodEntityEx goodEntityEx) {


            String goodsId     = "", address = "";
            String checkResult = "";

            if (!StringHelp.isEmpty(saveSaleCacheInfo.getName())) {
                if (!CollectionHelp.isEmpty(steelyardInfo.getBreedTo())) {
                    List<BreedTo> breedTos = steelyardInfo.getBreedTo();
                    for (BreedTo breedTo : breedTos) {
                        if (saveSaleCacheInfo.getName().equals(breedTo.goods_name)) {
                            goodsId = ("" + breedTo.goods_id);
                            address = (breedTo.address);
                            checkResult = breedTo.checkResult;
                        }
                    }
                }
            }

            if (StringHelp.isEmpty(goodsId)) {
                goodsId = ("0");
            }

            OrderEntity.GoodEntity goodEntity = new OrderEntity.GoodEntity(goodsId, saveSaleCacheInfo.getName(), saveSaleCacheInfo.getUnitPrice(), saveSaleCacheInfo.getWeight(), address, 0, 0, 0, 0, saveSaleCacheInfo.getUnitType(), goodEntityEx);

            goodEntity.checkResult = checkResult;
            return goodEntity;
        }


        public GoodEntity(String goodId, String goodName, float unitPrice, float weight, String address, int ad, int zeroAD, int offsetZeroAD, int kValue, int unitType, GoodEntityEx goodEntityEx) {
            this.unitPrice = unitPrice;
            this.weight = weight;
            this.goodName = StringHelp.getNotEmpty(goodName, "");
            this.goodId = StringHelp.getNotEmpty(goodId, "");
            ;
            this.address = StringHelp.getNotEmpty(address, "");
            ;
            this.ad = ad;
            this.zeroAD = zeroAD;
            this.offsetZeroAD = offsetZeroAD;
            this.kValue = kValue;
            this.unitType = unitType;
            this.itemId = System.currentTimeMillis();
        }

        public float calPrice() {
//            //float相乘的话5.00f*1.987f=9.934999，正确应该是9.935。
////            BigDecimal 是不接收float类型的 所以0.01f会变成double【会被补位 所以new BigDecimal(0.01f)精度出问题】


//                    new BigDecimal(""+unitPrice).multiply(new BigDecimal(""+weight)).floatValue()


            return SteelardHelp.calPrice(unitPrice, weight);
        }


        @SerializedName("ct")
        public long itemId;


        public long getCreateTimestamp() {
            return itemId;
        }


        @SerializedName("img_on")
        public String img_on;

        @SerializedName("ut")
        public int unitType;

        @SerializedName("up")
        public float unitPrice;

        @SerializedName("w")
        public float weight;


        @SerializedName("ad")
        public int ad;


        @SerializedName("zad")
        public int zeroAD;


        @SerializedName("ozad")
        public int offsetZeroAD;

        @SerializedName("k")
        public int kValue;

        @SerializedName("n")
        public String goodName;

        @SerializedName("id")
        public String goodId;

        @SerializedName("address")
        public String address;

        @SerializedName("gd")
        public String goodCode;


    }


}
